import pymysql

db=pymysql.connect(host='localhost',user='root',password='root',port=3306,db='spiders')
data={
    'user':'Bob',
    'age':25
}
table='students'
keys=','.join(data.keys())
values=','.join(['%s']*len(data))
cursor=db.cursor()
sql='INSERT INTO {table}({keys}) values({values})'.format(table=table,keys=keys,values=values)

try:
    if cursor.execute(sql,tuple(data.values())):
        print('Successful')
        db.commit()
except:
    print('Failed')
    db.rollback()
db.close()